package xhe.lib.log;

import xhe.entities.ApiEntity;
import xhe.lib.data.Util;
import xhe.lib.date.DateTime;
import xhe.lib.db.BaseFactory;

/**
 * <b> 类: </b> DataCenterLog.java
 * 
 * @author xhe
 * @version V1.0.0.0
 * @version --2020年10月28日
 */
public class DataCenter {

	public static boolean interfaceLog(ApiEntity requestEntity) {

		boolean blResult = false;

		try {

			requestEntity.setResponseTime(DateTime.toString(DateTime.now(), "yyyy-MM-dd HH:mm:ss"));

			BaseFactory oBaseFactory = new BaseFactory(requestEntity);

			String strSql = "insert into t_logs_interface ";
			strSql += "( ";
			strSql += "		loginname, ";
			strSql += "		username, ";
			strSql += "		method, ";
			strSql += "		token, ";
			strSql += "		clientip, ";
			strSql += "		requesttime, ";
			strSql += "		responsetime, ";
			strSql += "		executetime, ";
			strSql += "		requestparams, ";
			strSql += "		responsetext, ";
			strSql += "		status, ";
			strSql += "		message ";
			strSql += ") ";
			strSql += "values ";
			strSql += "( ";
			strSql += "    	'" + requestEntity.getLoginName() + "', ";
			strSql += "    	'" + requestEntity.getUserName() + "',";
			strSql += "     '" + requestEntity.getMethodName() + "', ";
			strSql += "    	'" + requestEntity.getToken() + "', ";
			strSql += "    	'" + requestEntity.getClientIP() + "', ";
			strSql += "     '" + requestEntity.getRequestTime() + "', ";
			strSql += "    	'" + requestEntity.getResponseTime() + "', ";
			strSql += "    	" + requestEntity.getExecuteTime() + ",";
			strSql += "     '" + Util.decodeSpecialSymbol(requestEntity.getParams()) + "', ";
			strSql += "     '" + Util.decodeSpecialSymbol(requestEntity.getResponse()) + "',";
			strSql += "     " + requestEntity.getCode().getCode() + ",";
			strSql += "     '" + requestEntity.getMessage() + "' ";
			strSql += ")";

			System.out.println("SQL:" + strSql);

			// 执行语句
			int iResult = oBaseFactory.Execute(strSql, requestEntity);

			// 封装返回对象
			if (iResult > 0) {

				blResult = true;
			}
		} catch (Exception ex) {

		}
		return blResult;
	}

	public static boolean operateLog(ApiEntity requestEntity) {

		boolean blResult = false;

		try {

			if (requestEntity.getWriteLog()) {
				BaseFactory oBaseFactory = new BaseFactory(requestEntity);

				String strSql = "insert into t_logs_operate ";
				strSql += "( ";
				strSql += "		tablename, ";
				strSql += "		primarykey, ";
				strSql += "		primaryvalue, ";
				strSql += "		content, ";
				strSql += "		operatetype, ";
				strSql += "		operatetime, ";
				strSql += "		operateuser ";
				strSql += ") ";
				strSql += "values ";
				strSql += "( ";
				strSql += "    	'" + requestEntity.getTableName() + "', ";
				strSql += "    	'" + requestEntity.getPrimaryKey() + "',";
				strSql += "    	'" + requestEntity.getPrimaryValue().replace("'", "") + "',";
				strSql += "     '" + requestEntity.getContent().replace("'", "") + "', ";
				strSql += "    	" + requestEntity.getOperateType() + ", ";
				strSql += "     '" + DateTime.toString(DateTime.now(), "yyyy-MM-dd HH:mm:ss") + "', ";
				strSql += "    	'" + requestEntity.getLoginName() + "' ";
				strSql += ")";

				System.out.println("SQL:" + strSql);

				// 执行语句
				int iResult = oBaseFactory.Execute(strSql, requestEntity);

				// 封装返回对象
				if (iResult > 0) {

					blResult = true;
				}
			} else {
				blResult = false;
			}
		} catch (Exception ex) {

		}
		return blResult;
	}
}
